package com.android.equipment.ui.dialog

import android.content.Context
import com.android.equipment.R
import com.android.equipment.adapter.SelectDeviceAdapter
import com.android.equipment.bean.WorkDetailDeviceData
import com.android.equipment.databinding.DialogSelectDeviceBinding
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.lxj.xpopup.core.BottomPopupView

class SelectDeviceDialog(context: Context, val deviceList: List<WorkDetailDeviceData>, val listener: SelectDeviceListener): BottomPopupView(context) {

    val binding: DialogSelectDeviceBinding by lazy {
        DialogSelectDeviceBinding.bind(popupImplView)
    }

    val selectDeviceAdapter: SelectDeviceAdapter by lazy {
        SelectDeviceAdapter(R.layout.item_device_select)
    }
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_select_device
    }

    override fun onCreate() {
        super.onCreate()
        initRecycler()
    }

    private fun initRecycler(){
        val flex = FlexboxLayoutManager(context).apply {
            flexDirection = FlexDirection.ROW
            flexWrap = FlexWrap.WRAP
            justifyContent = JustifyContent.FLEX_START
        }
        binding.rvDevice.layoutManager = flex
        binding.rvDevice.adapter = selectDeviceAdapter
        selectDeviceAdapter.setList(deviceList)
        selectDeviceAdapter.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position)
            listener.onSelectDeviceClick(item as WorkDetailDeviceData)
            dismiss()
        }
    }

    interface SelectDeviceListener {
        fun onSelectDeviceClick(device: WorkDetailDeviceData)
    }
}